package com.sas.web.panel;

import java.util.ArrayList;
import java.util.List;

import org.apache.wicket.Response;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.form.AjaxFormSubmitBehavior;
import org.apache.wicket.extensions.ajax.markup.html.autocomplete.AbstractAutoCompleteRenderer;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.panel.FeedbackPanel;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.PropertyModel;
import org.apache.wicket.spring.injection.annot.SpringBean;
import org.springframework.util.StringUtils;

import com.sas.domain.Article;
import com.sas.domain.Client;
import com.sas.model.EntityModel;
import com.sas.repository.ClientRepository;
import com.sas.web.panel.clients.ClientsShortListPanel;

public class SearchClientPanel extends Panel {

	/**
	 * 
	 */
	private static final long serialVersionUID = 2697427926433424981L;

	private static final int MAX_CONNECTIONS_PER_SEARCH = 50;
	
	@SpringBean
	private ClientRepository clientRepository;

	private String to;
	private List<IModel<Client>> searchList;
	private IModel<Article> articleModel;
	
	public SearchClientPanel(String id, final IModel<Article> articleModel) {
		super(id);
		this.articleModel = articleModel;
		Form<Void> form = new Form<Void>("form");
		
		List<IModel<Client>> articles_aux = new ArrayList<IModel<Client>>();
		for  (Client a: clientRepository.findAll()) {
			articles_aux.add(new EntityModel<Client>(Client.class, a));
		}
		
		final List<IModel<Client>> articles = articles_aux;
		AbstractAutoCompleteRenderer<IModel<Client>> autoCompleteRenderer = new AbstractAutoCompleteRenderer<IModel<Client>>() {
			/**
			 * 
			 */
			private static final long serialVersionUID = 3289011960908027499L;

			@Override
			protected String getTextValue(IModel<Client> object) {
				return object.getObject().getCuilCuit();
			}

			@Override
			protected void renderChoice(IModel<Client> object,
					Response response, String criteria) {
				response.write(getTextValue(object));
				
			}
			
		};

		// textfield
		AbstractAutoCompleteTextField<IModel<Client>> toField = new AbstractAutoCompleteTextField<IModel<Client>>("toField", new PropertyModel<String>(this, "to"), autoCompleteRenderer) {
			/**
			 * 
			 */
			private static final long serialVersionUID = -3799794995432360916L;

			protected final List<IModel<Client>> getChoiceList(final String input) {
				return getConnectionsSubsetForSearch(articles, input);
			}

			public List<IModel<Client>> getList() {
				return searchList;
			}

			@Override
			protected String getChoiceValue(IModel<Client> choice)
					throws Throwable {
				return choice.getObject().getCuilCuit();
			}
		};

		form.add(toField);

		

        toField.add(new AjaxFormSubmitBehavior(form, "onchange") {

			/**
			 * 
			 */
			private static final long serialVersionUID = -168215546120542218L;

			@Override
			protected void onError(AjaxRequestTarget arg0) {
				// TODO Auto-generated method stub
				
			}

			@Override
			protected void onSubmit(AjaxRequestTarget target) {
				final ClientsShortListPanel clientsShortListPanel = new ClientsShortListPanel("clientsShortListPanel", searchList, articleModel);
				target.getPage().addOrReplace(clientsShortListPanel);
				setResponsePage(target.getPage());
				
			}
        });
		
		FeedbackPanel feedback = new FeedbackPanel("feedback");
		add(feedback);
		add(form);
	}


	protected List<IModel<Client>> getConnectionsSubsetForSearch(
			List<IModel<Client>> clients, String input) {
		List<IModel<Client>> subList = new ArrayList<IModel<Client>>();
		for (IModel<Client> clientModel: clients) {
			if (subList.size() == MAX_CONNECTIONS_PER_SEARCH) {
				break;
			}
			
			Client c = clientModel.getObject();
			
			if (StringUtils.startsWithIgnoreCase(c.getName(), input)) {
				subList.add(clientModel);
			} else if (StringUtils.startsWithIgnoreCase(c.getCuilCuit(), input)) {
				subList.add(clientModel);
			} else if (c.getTelephone() != null 
					&& StringUtils.startsWithIgnoreCase(c.getTelephone().getTelephoneNumber().toString(), input)) {
				subList.add(clientModel);
			} else if (c.getAlternative() != null
					&& StringUtils.startsWithIgnoreCase(c.getTelephone().getTelephoneNumber().toString(), input)) {
				subList.add(clientModel);
			} else if (c.getCellphone() != null
					&& StringUtils.startsWithIgnoreCase(c.getCellphone().getTelephoneNumber().toString(), input)) {
				subList.add(clientModel);
			} else if (c.getEmail() != null
					&& StringUtils.startsWithIgnoreCase(c.getEmail(), input)) {
				subList.add(clientModel);
			}
		}
		
		searchList = subList;
		return subList;
	}
}
